package com.atguigu.gulimall.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车vo
 */
public class Cart {

    private List<CartItem> items;

    //商品总数量
    private Integer countNum;

    //商品类型的数量
    private Integer countType;

    //商品总价
    private BigDecimal totalAmount;

    //减免价格，默认0
    private BigDecimal reduce = BigDecimal.ZERO;

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> itemVos) {
        this.items = itemVos;
    }

    /**
     * 计算商品总数量
     *
     * @return
     */
    public Integer getCountNum() {
        if (CollectionUtils.isEmpty(items))
            return 0;
        return items.stream().mapToInt(item -> item.getCount()).sum();
    }

    /**
     * 计算类型数量
     *
     * @return
     */
    public Integer getCountType() {
        if (CollectionUtils.isEmpty(items))
            return 0;
        return items.size();
    }

    /**
     * 计算所有商品总价
     *
     * @return
     */
    public BigDecimal getTotalAmount() {
        if (CollectionUtils.isEmpty(items))
            return BigDecimal.ZERO;

        //计算购物项选中的总价
        BigDecimal reduce = items.stream().map(item -> item.getCheck() ? item.getTotalPrice() : BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add);

        //总价大于等于优惠价？总价减去优惠价：0
        BigDecimal bigDecimal = reduce.compareTo(getReduce()) > -1 ? reduce.subtract(getReduce()) : BigDecimal.ZERO;
        return bigDecimal;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
